package logr

import (
	"awesome-runner/types"
	"errors"
	"io/ioutil"
	"net/http"
)

func RequestGet(url string, header types.RequestHeader, symbol bool) (string, error) {
	client := http.Client{}
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return "", err
	}

	req.Header.Add("Content-type", "application/json;charset=utf-8")
	if header.Name != "" {
		req.Header.Add(header.Name, header.Value)
	}
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	if symbol {
		// 需求解析
		return "", nil
	} else {
		b, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return "", err
		}
		var parseMessage types.GitlabResponseMessage
		err = JSON.Unmarshal(b, &parseMessage)
		if err != nil {
			return string(b), nil
		} else {
			return "", errors.New(parseMessage.Message)
		}
	}
}
